package cn.blog.controller.front;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.blog.controller.BaseServlet;
import cn.blog.entity.addBean.RzglArtInfo;
import cn.blog.service.front.ChaxunService;
import cn.blog.service.front.CompileHeadService;
import cn.blog.service.front.RizhiglService;

@WebServlet("/front/caogao")
public class CaogaoServlet extends BaseServlet {
	boolean isStr2Num(String str) {
		try {
			Integer.parseInt(str);
			return true;
		} catch (NumberFormatException e) {
			return false;
		}
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		CompileHeadService chs = new CompileHeadService();
		// 获取用户id
		String userId = req.getSession().getAttribute("userId").toString();
		int uid = Integer.parseInt(userId);
		ChaxunService cs = new ChaxunService();
		String aid = req.getParameter("caogaoartId");

		String state = req.getParameter("state");
		if (state.equals("2")) {
			state = "已发表";
		} else if (state.equals("1")) {
			state = "作废";
		}
		cs.updateState(state, Integer.parseInt(aid));
		RizhiglService rs = new RizhiglService();
		List<RzglArtInfo> art = rs.getArticleAll(uid, "草稿");// 调用查询所有博客文章方法

		PrintWriter out = resp.getWriter();

		String artJson = "";
		for (int i = 0; i < art.size(); i++) {
			/*
			 * String list =
			 * art.get(i).getArtId()+","+art.get(i).getTitle()+","+art.get(i).getArtClass()+
			 * "," +art.get(i).getArtTime()+",";
			 */
			artJson += art.get(i).getArtId() + "," + art.get(i).getTitle() + "," + art.get(i).getArtClass() + ","
					+ art.get(i).getArtTime() + ";";
			/* artJson = artJson+list; */
		}
		// 截取字符串，去掉最后的逗号
		if(artJson.length() !=0) {
			artJson = artJson.substring(0, artJson.length() - 1);
		}
		
		/*
		 * //去掉城市json的最后一个字符 artJson = artJson.substring(0, artJson.length());
		 */
		// 将json格式的字符串输出
		resp.setCharacterEncoding("utf-8");
		out.print(artJson);
	}
}
